Game xếp hình Tetris C#

1 using System;
2 using
System.Drawing;
3 using
System.Collections;
4 using
System.ComponentModel;
5 using
System.Windows.Forms;
6 using
System.Data;
7
8 namespace
Tetris
9 {

10     ///
<summary>
11     ///
Descripción breve de Form1.
12     ///
</summary>
13     
public class frmGUI : System.Windows.Forms.Form
14     {
15
16         
private System.Windows.Forms.PictureBox pbPantallaJuego;
17         
private System.Windows.Forms.Timer timer1;
18         
private System.ComponentModel.IContainer components;
19         
private Tetris t;
20         
private bool haciaAbajo;
21         
private bool haciaDerecha;
22         
private bool rotaDerecha;
23         
private bool rotaIzquierda;
24         
private bool enPausa;
25         
private System.Windows.Forms.Timer timer2;
26         
private System.Windows.Forms.Label lblNumLineas;
27         
private System.Windows.Forms.Label lblNivel;
28         
private System.Windows.Forms.Label label2;
29         
private System.Windows.Forms.Label label1;
30         
private System.Windows.Forms.MainMenu mainMenu1;
31         
private System.Windows.Forms.MenuItem menuItem1;
32         
private System.Windows.Forms.MenuItem menuItem2;
33         
private System.Windows.Forms.MenuItem menuItem3;
34         
private System.Windows.Forms.MenuItem menuItem4;
35         
private System.Windows.Forms.MenuItem menuItem5;
36         
private System.Windows.Forms.Label label3;
37         
private System.Windows.Forms.PictureBox pbPiezaSiguiente;
38         
private System.Windows.Forms.MenuItem menuItem6;
39         
private System.Windows.Forms.Label label4;
40         
private System.Windows.Forms.Label label5;
41         
private System.Windows.Forms.Label label6;
42         
private System.Windows.Forms.Label label7;
43         
private System.Windows.Forms.Label label8;
44         
private System.Windows.Forms.Label label9;
45         
private System.Windows.Forms.Label lblPuntuacion;
46         
private bool haciaIzquierda;
47
48         
public frmGUI()
49         {
50             
//
51             
// Necesario para admitir el Diseñador de Windows Forms
52             
//
53             InitializeComponent();
54
55             
//
56             
// TODO: agregar código de constructor después de llamar a InitializeComponent
57             
//
58         }

59
60         ///
<summary>
61         ///
Limpiar los recursos que se estén utilizando.
62         ///
</summary>
63         
protected override void Dispose( bool disposing )
64         {
65             
if( disposing )
66             {
67                 
if (components != null)
68                 {
69                     components.Dispose();
70                 }
71             }
72             
base.Dispose( disposing );
73         }
74
75         
#region Código generado por el Diseñador de Windows Forms
76         ///
<summary>
77         ///
Método necesario para admitir el Diseñador. No se puede modificar
78         ///
el contenido del método con el editor de código.
79         ///
</summary>
80         
private void InitializeComponent()
81         {
82             
this.components = new System.ComponentModel.Container();
83             
this.pbPantallaJuego = new System.Windows.Forms.PictureBox();
84             
this.timer1 = new System.Windows.Forms.Timer(this.components);
85             
this.timer2 = new System.Windows.Forms.Timer(this.components);
86             
this.lblNumLineas = new System.Windows.Forms.Label();
87             
this.lblNivel = new System.Windows.Forms.Label();
88             
this.label2 = new System.Windows.Forms.Label();
89             
this.label1 = new System.Windows.Forms.Label();
90             
this.mainMenu1 = new System.Windows.Forms.MainMenu();
91             
this.menuItem1 = new System.Windows.Forms.MenuItem();
92             
this.menuItem3 = new System.Windows.Forms.MenuItem();
93             
this.menuItem6 = new System.Windows.Forms.MenuItem();
94             
this.menuItem4 = new System.Windows.Forms.MenuItem();
95             
this.menuItem2 = new System.Windows.Forms.MenuItem();
96             
this.menuItem5 = new System.Windows.Forms.MenuItem();
97             
this.pbPiezaSiguiente = new System.Windows.Forms.PictureBox();
98             
this.label3 = new System.Windows.Forms.Label();
99             
this.label4 = new System.Windows.Forms.Label();
100             
this.label5 = new System.Windows.Forms.Label();
101             
this.label6 = new System.Windows.Forms.Label();
102             
this.label7 = new System.Windows.Forms.Label();
103             
this.label8 = new System.Windows.Forms.Label();
104             
this.label9 = new System.Windows.Forms.Label();
105             
this.lblPuntuacion = new System.Windows.Forms.Label();
106             
this.SuspendLayout();
107             
//
108             
// pbPantallaJuego
109             
//
110             
this.pbPantallaJuego.BackColor = System.Drawing.Color.Black;
111             
this.pbPantallaJuego.Location = new System.Drawing.Point(168, 24);
112             
this.pbPantallaJuego.Name = "pbPantallaJuego";
113             
this.pbPantallaJuego.Size = new System.Drawing.Size(260, 420);
114             
this.pbPantallaJuego.TabIndex = 0;
115             
this.pbPantallaJuego.TabStop = false;
116             
//
117             
// timer1
118             
//
119             
this.timer1.Interval = 800;
120             
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
121             
//
122             
// timer2
123             
//
124             
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
125             
//
126             
// lblNumLineas
127             
//
128             
this.lblNumLineas.BackColor = System.Drawing.Color.Black;
129             
this.lblNumLineas.ForeColor = System.Drawing.Color.White;
130             
this.lblNumLineas.Location = new System.Drawing.Point(16, 152);
131             
this.lblNumLineas.Name = "lblNumLineas";
132             
this.lblNumLineas.TabIndex = 1;
133             
this.lblNumLineas.Text = "0";
134             
this.lblNumLineas.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
135             
//
136             
// lblNivel
137             
//
138             
this.lblNivel.BackColor = System.Drawing.Color.Black;
139             
this.lblNivel.ForeColor = System.Drawing.Color.White;
140             
this.lblNivel.Location = new System.Drawing.Point(16, 208);
141             
this.lblNivel.Name = "lblNivel";
142             
this.lblNivel.TabIndex = 2;
143             
this.lblNivel.Text = "1";
144             
this.lblNivel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
145             
//
146             
// label2
147             
//
148             
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
149             
this.label2.Location = new System.Drawing.Point(16, 192);
150             
this.label2.Name = "label2";
151             
this.label2.Size = new System.Drawing.Size(40, 16);
152             
this.label2.TabIndex = 4;
153             
this.label2.Text = "Nivel:";
154             
//
155             
// label1
156             
//
157             
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
158             
this.label1.Location = new System.Drawing.Point(16, 136);
159             
this.label1.Name = "label1";
160             
this.label1.Size = new System.Drawing.Size(40, 16);
161             
this.label1.TabIndex = 5;
162             
this.label1.Text = "Lineas:";
163             
//
164             
// mainMenu1
165             
//
166             
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
167                                                                                       
this.menuItem1,
168                                                                                       
this.menuItem2});
169             
//
170             
// menuItem1
171             
//
172             
this.menuItem1.Index = 0;
173             
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
174                                                                                       
this.menuItem3,
175                                                                                       
this.menuItem6,
176                                                                                       
this.menuItem4});
177             
this.menuItem1.Text = "Juego";
178             
//
179             
// menuItem3
180             
//
181             
this.menuItem3.Index = 0;
182             
this.menuItem3.Text = "Nuevo";
183             
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
184             
//
185             
// menuItem6
186             
//
187             
this.menuItem6.Index = 1;
188             
this.menuItem6.Text = "Pausa";
189             
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
190             
//
191             
// menuItem4
192             
//
193             
this.menuItem4.Index = 2;
194             
this.menuItem4.Text = "Salir";
195             
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
196             
//
197             
// menuItem2
198             
//
199             
this.menuItem2.Index = 1;
200             
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
201                                                                                       
this.menuItem5});
202             
this.menuItem2.Text = "?";
203             
//
204             
// menuItem5
205             
//
206             
this.menuItem5.Index = 0;
207             
this.menuItem5.Text = "Acerca de...";
208             
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
209             
//
210             
// pbPiezaSiguiente
211             
//
212             
this.pbPiezaSiguiente.BackColor = System.Drawing.Color.Black;
213             
this.pbPiezaSiguiente.Location = new System.Drawing.Point(16, 40);
214             
this.pbPiezaSiguiente.Name = "pbPiezaSiguiente";
215             
this.pbPiezaSiguiente.Size = new System.Drawing.Size(80, 80);
216             
this.pbPiezaSiguiente.TabIndex = 7;
217             
this.pbPiezaSiguiente.TabStop = false;
218             
//
219             
// label3
220             
//
221             
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
222             
this.label3.Location = new System.Drawing.Point(16, 24);
223             
this.label3.Name = "label3";
224             
this.label3.Size = new System.Drawing.Size(88, 16);
225             
this.label3.TabIndex = 8;
226             
this.label3.Text = "Próxima pieza:";
227             
//
228             
// label4
229             
//
230             
this.label4.Location = new System.Drawing.Point(8, 304);
231             
this.label4.Name = "label4";
232             
this.label4.Size = new System.Drawing.Size(144, 16);
233             
this.label4.TabIndex = 9;
234             
this.label4.Text = "Flecha Arriba -> Rotar";
235             
//
236             
// label5
237             
//
238             
this.label5.Location = new System.Drawing.Point(8, 320);
239             
this.label5.Name = "label5";
240             
this.label5.Size = new System.Drawing.Size(144, 16);
241             
this.label5.TabIndex = 10;
242             
this.label5.Text = "Flecha Abajo -> Bajar";
243             
//
244             
// label6
245             
//
246             
this.label6.Location = new System.Drawing.Point(8, 336);
247             
this.label6.Name = "label6";
248             
this.label6.Size = new System.Drawing.Size(144, 16);
249             
this.label6.TabIndex = 11;
250             
this.label6.Text = "Flecha Izq -> Mover Izq";
251             
//
252             
// label7
253             
//
254             
this.label7.Location = new System.Drawing.Point(8, 352);
255             
this.label7.Name = "label7";
256             
this.label7.Size = new System.Drawing.Size(152, 16);
257             
this.label7.TabIndex = 12;
258             
this.label7.Text = "Flecha Dcha -> Mover Dcha";
259             
//
260             
// label8
261             
//
262             
this.label8.Location = new System.Drawing.Point(8, 368);
263             
this.label8.Name = "label8";
264             
this.label8.Size = new System.Drawing.Size(132, 12);
265             
this.label8.TabIndex = 13;
266             
this.label8.Text = "P -> Pausa";
267             
//
268             
// label9
269             
//
270             
this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
271             
this.label9.Location = new System.Drawing.Point(16, 240);
272             
this.label9.Name = "label9";
273             
this.label9.Size = new System.Drawing.Size(72, 16);
274             
this.label9.TabIndex = 15;
275             
this.label9.Text = "Puntuación:";
276             
//
277             
// lblPuntuacion
278             
//
279             
this.lblPuntuacion.BackColor = System.Drawing.Color.Black;
280             
this.lblPuntuacion.ForeColor = System.Drawing.Color.White;
281             
this.lblPuntuacion.Location = new System.Drawing.Point(16, 256);
282             
this.lblPuntuacion.Name = "lblPuntuacion";
283             
this.lblPuntuacion.TabIndex = 14;
284             
this.lblPuntuacion.Text = "0";
285             
this.lblPuntuacion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
286             
//
287             
// frmGUI
288             
//
289             
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
290             
this.ClientSize = new System.Drawing.Size(440, 473);
291             
this.Controls.Add(this.label9);
292             
this.Controls.Add(this.lblPuntuacion);
293             
this.Controls.Add(this.label8);
294             
this.Controls.Add(this.label7);
295             
this.Controls.Add(this.label6);
296             
this.Controls.Add(this.label5);
297             
this.Controls.Add(this.label4);
298             
this.Controls.Add(this.label3);
299             
this.Controls.Add(this.pbPiezaSiguiente);
300             
this.Controls.Add(this.label1);
301             
this.Controls.Add(this.label2);
302             
this.Controls.Add(this.lblNivel);
303             
this.Controls.Add(this.lblNumLineas);
304             
this.Controls.Add(this.pbPantallaJuego);
305             
this.Menu = this.mainMenu1;
306             
this.Name = "frmGUI";
307             
this.Text = "eTetris";
308             
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmGUI_KeyDown);
309             
this.Load += new System.EventHandler(this.frmGUI_Load);
310             
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.frmGUI_KeyUp);
311             
this.ResumeLayout(false);
312
313         }
314         
#endregion
315
316         ///
<summary>
317         ///
Punto de entrada principal de la aplicación.
318         ///
</summary>
319         
[STAThread]
320         
static void Main()
321         {
322             Application.Run(
new frmGUI());
323         }
324
325         
private void frmGUI_Load(object sender, System.EventArgs e)
326         {
327             t =
new Tetris();
328         }
329
330         
private void frmGUI_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
331         {
332             
string strKeyPress = null;
333             strKeyPress = e.KeyCode.ToString();
334             
if (!t.juegoTerminado)
335             {
336                 
switch(strKeyPress.ToUpper())
337                 {
338                     
case "LEFT":
339                         haciaIzquierda =
true;
340                         
//t.muevePiezaIzquierda();
341                         
//pintaPantalla(t.matrizPantalla);
342                         
break;
343                     
case "RIGHT":
344                         haciaDerecha =
true;
345                         
//t.muevePiezaDerecha();
346                         
//pintaPantalla(t.matrizPantalla);
347                         
break;
348                     
case "UP":
349                         rotaDerecha =
true;
350                         
//t.rotaPiezaDerecha();
351                         
//pintaPantalla(t.matrizPantalla);
352                         
break;
353                     
case "A":
354                         rotaIzquierda =
true;
355                         
//t.rotaPiezaIzquierda();
356                         
//pintaPantalla(t.matrizPantalla);
357                         
break;
358                     
case "DOWN":
359                         haciaAbajo =
true;
360                         
//t.muevePiezaAbajo();
361                         
//pintaPantalla(t.matrizPantalla);
362                         
break;
363                     
case "P":
364                         pausaJuego();
365                         
break;
366                     
default:
367                         
//MessageBox.Show(strKeyPress.ToUpper());
368                         
break;
369                 }
370             }
371             
else
372             {
373                 
switch(strKeyPress.ToUpper())
374                 {
375                     
case "ENTER":
376
377                         
break;
378                     
default:
379                         
break;
380                 }
381             }
382         }
383
384         
private void frmGUI_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
385         {
386             
string strKeyPress = null;
387             strKeyPress = e.KeyCode.ToString();
388             
if (!t.juegoTerminado)
389             {
390                 
switch(strKeyPress.ToUpper())
391                 {
392                     
case "LEFT":
393                         haciaIzquierda =
false;
394                         
break;
395                     
case "RIGHT":
396                         haciaDerecha =
false;
397                         
break;
398                     
case "DOWN":
399                         haciaAbajo =
false;
400                         
break;
401                     
case "UP":
402                         rotaDerecha =
false;
403                         
break;
404                     
case "A":
405                         rotaIzquierda =
false;
406                         
break;
407                     
default:
408                         
//MessageBox.Show(strKeyPress.ToUpper());
409                         
break;
410                 }
411             }
412         }
413
414         
public void pintaPantalla(int [, ] matrizPantalla)
415         {
416             Bitmap B =
new Bitmap(pbPantallaJuego.Width, pbPantallaJuego.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
417             Graphics G = Graphics.FromImage(B);
418             G.Clear(Color.Black);
419             
for (int x = 0; x < Constantes.COLUMNAS_PANTALLA; x++)
420             {
421                 
for (int y = 0; y < Constantes.FILAS_PANTALLA; y++)
422                 {
423                     
int elemento = matrizPantalla[y, x];
424                     
if (elemento != 0)
425                     {
426                         dibujaCuadro(G, y, x, Constantes.COLORES(elemento -
1));
427                     }
428                 }
429             }
430             pbPantallaJuego.Image = B;
431         }
432         
433         
public void pintaPiezaSiguiente(Pieza p)
434         {
435             Bitmap B =
new Bitmap(pbPiezaSiguiente.Width, pbPiezaSiguiente.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
436             Graphics G = Graphics.FromImage(B);
437             G.Clear(Color.Black);
438             
for (int x = 0; x < Constantes.COLUMNAS_PIEZAS; x++)
439             {
440                 
for (int y = 0; y < Constantes.FILAS_PIEZAS; y++)
441                 {
442                     
int elemento = t.piezaSiguiente[y, x];
443                     
if (elemento != 0)
444                     {
445                         dibujaCuadro(G, y, x, Constantes.COLORES(t.piezaSiguiente.color -
1));
446                     }
447                 }
448             }
449             pbPiezaSiguiente.Image = B;
450         }
451
452         
private void dibujaCuadro(Graphics G, int Y,int X, Color C)
453         {
454             
int x = (X * Constantes.ANCHO_CELDA) + 1;
455             
int y = (Y * Constantes.ALTO_CELDA) + 1;
456             SolidBrush Br =
new SolidBrush(C);
457             G.FillRectangle(Br, x, y, Constantes.ANCHO_CELDA -
2, Constantes.ALTO_CELDA - 2);
458         }
459
460         
private void timer1_Tick(object sender, System.EventArgs e)
461         {
462             
if (t.numLineas < Constantes.NUM_LINEAS_POR_NIVEL)
463             {
464                 lblNumLineas.Text = t.numLineas.ToString();
465                 lblPuntuacion.Text = t.puntuacion.ToString();
466                 
if (haciaAbajo)
467                     actualizaPantalla();
468                 
else
469                 {
470                     haciaAbajo =
true;
471                     actualizaPantalla();
472                     haciaAbajo =
false;
473                 }
474                 pintaPiezaSiguiente(t.piezaSiguiente);
475                 
if (t.juegoTerminado)
476                 {
477                     timer1.Stop();
478                     MessageBox.Show(
"Se terminó el juego");
479                 }
480             }
481         }
482
483         
private void timer2_Tick(object sender, System.EventArgs e)
484         {
485             
if (t.numLineas < Constantes.NUM_LINEAS_POR_NIVEL)
486             {
487                 actualizaPantalla();
488             }
489             
else
490             {
491                 timer2.Stop();
492                 timer1.Stop();
493                 lblNumLineas.Text = t.numLineas.ToString();
494                 lblPuntuacion.Text = t.puntuacion.ToString();
495                 inicializaTeclas();
496                 t.nuevoNivel();
497                 MessageBox.Show(
"¡¡Enhorabuena!! Has pasado al nivel " + (t.nivel + 1));
498                 lblNivel.Text = ((
int)t.nivel + 1).ToString();
499                 timer1.Interval = Constantes.NIVELES(t.nivel);
500                 timer2.Start();
501                 timer1.Start();
502             }
503             
if (t.juegoTerminado)
504             {
505                 timer2.Stop();
506             }
507         }
508
509         
private void actualizaPantalla()
510         {
511             
if (haciaAbajo)
512             {
513                 t.muevePiezaAbajo();
514             }
515             
if (haciaDerecha)
516             {
517                 t.muevePiezaDerecha();
518             }
519             
if (haciaIzquierda)
520             {
521                 t.muevePiezaIzquierda();
522             }
523             
if (rotaDerecha)
524             {
525                 t.rotaPiezaDerecha();
526                 rotaDerecha =
false;
527             }
528             
if (rotaIzquierda)
529             {
530                 t.rotaPiezaIzquierda();
531                 rotaIzquierda =
false;
532             }
533             
534             pintaPantalla(t.matrizPantalla);
535         }
536
537         
private void inicializaTeclas()
538         {
539             haciaDerecha =
false;
540             haciaIzquierda =
false;
541             rotaDerecha =
false;
542             rotaIzquierda =
false;
543             haciaAbajo =
false;
544             enPausa =
false;
545         }
546
547         
private void nuevoJuego()
548         {
549             inicializaTeclas();
550             t.nuevoJuego();
551             lblPuntuacion.Text =
"0";
552             lblNivel.Text =
"1";
553             timer1.Interval = Constantes.NIVELES(t.nivel);
554             pintaPantalla(t.matrizPantalla);
555             timer1.Start();
556             timer2.Start();
557         }
558
559         
private void pausaJuego()
560         {
561             
if (!t.juegoTerminado)
562             {
563                 
if (!enPausa)
564                 {
565                     timer1.Stop();
566                     timer2.Stop();
567                 }
568                 
else
569                 {
570                     timer1.Start();
571                     timer2.Start();
572                 }
573                 enPausa = !enPausa;
574             }
575         }
576
577         
private void menuItem4_Click(object sender, System.EventArgs e)
578         {
579             
this.Close();
580         }
581
582         
private void menuItem3_Click(object sender, System.EventArgs e)
583         {
584             nuevoJuego();
585         }
586
587         
private void menuItem5_Click(object sender, System.EventArgs e)
588         {
589             enPausa =
false;
590             pausaJuego();
591             AcercaDe a =
new AcercaDe();
592             a.ShowDialog();
593             a.Dispose();
594             enPausa =
true;
595             pausaJuego();
596         }
597
598         
private void menuItem6_Click(object sender, System.EventArgs e)
599         {
600             pausaJuego();
601         }
602
603     }
604 }



Game xếp hình Tetris C# 5.855 lượt xem

Gõ tìm kiếm nhanh...